﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SnakeController : MonoBehaviour
{
    public Transform BoundaryA, BoundaryB;
    public float moveSpeed;
    private float faceDirection, left, right;
    // Start is called before the first frame update
    void Start()
    {
        faceDirection = 1;
        right = BoundaryA.position.x >= BoundaryB.position.x ? BoundaryA.position.x : BoundaryB.position.x;
        left = BoundaryA.position.x < BoundaryB.position.x ? BoundaryA.position.x : BoundaryB.position.x;

        Destroy(BoundaryA.gameObject);
        Destroy(BoundaryB.gameObject);
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        if (transform.position.x < left)
            faceDirection = 1;
        if (transform.position.x > right)
            faceDirection = -1;

        transform.localScale = new Vector3(faceDirection, 1, 1);
        transform.position = new Vector3(transform.position.x + moveSpeed * Time.fixedDeltaTime * faceDirection, 
                                         transform.position.y, 0);
    }
}
